package com.dreamer.view.lottery;

import com.dreamer.domain.lottery.Gift;
import com.dreamer.domain.lottery.Player;
import com.dreamer.domain.lottery.PlayerVo;
import com.dreamer.domain.user.User;
import com.dreamer.service.lottery.GiftHandler;
import com.dreamer.service.lottery.LotteryHandler;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import ps.mx.otter.utils.WebUtil;
import ps.mx.otter.utils.message.Message;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class LotteryController {

    @Autowired
    private LotteryHandler lotteryHandler;

    @Autowired
    private GiftHandler giftHandler;

    @RequestMapping("/lottery")
    public String toLottery(Integer lid, Model model, HttpServletRequest request) {
        //查找当前用户
        User user = (User) WebUtil.getCurrentUser(request);
        //查找抽奖次数
        Integer count = lotteryHandler.getLotteryCount(user.getId(), lid);
        //查找抽奖记录
        List<Player> players = lotteryHandler.getGiftResults(user.getId(), lid);
        List<PlayerVo> playerVos = new ArrayList<>();
        players.forEach(player -> {
            PlayerVo playerVo = new PlayerVo();
            playerVo.setName(user.getNickName() + "-识别码:" + user.getId());
            if(player.getGid()==null){
                playerVo.setContent("再接再厉");
                playerVo.setSort(-1);
            }else {
                Gift gift = giftHandler.get(player.getGid());
                playerVo.setContent(gift.getName());
                playerVo.setSort(gift.getSort());
            }
            playerVos.add(playerVo);
        });
        model.addAttribute("count", count);
        model.addAttribute("playerVos", playerVos);
        return "/lottery/lottery";
    }


    @RequestMapping("/gift")
    @ResponseBody
    public Message getGift(HttpServletRequest request, Integer lid) {
        try {
            User user = (User) WebUtil.getCurrentUser(request);
            Player player = lotteryHandler.getGift(user.getId(), lid);
            PlayerVo playerVo = new PlayerVo();
            playerVo.setName(user.getNickName() + "-识别码:" + user.getId());
            if (player.getGid() == null) {
                playerVo.setContent("再接再厉");
                playerVo.setSort(-1);
            } else {
                Gift gift = giftHandler.get(player.getGid());
                playerVo.setContent(gift.getName());
                playerVo.setSort(gift.getSort());
            }
            Message message = Message.createSuccessMessage();
            message.setData(JSONObject.fromObject(playerVo).toString());
            return message;
        } catch (Exception e) {
            e.printStackTrace();
            return Message.createFailedMessage(e.getMessage());
        }

    }


}
